﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Drawing;
using System.Text.RegularExpressions;

public partial class WebUserControl_ControlDangKyTuVanVien : System.Web.UI.UserControl
{

    ClassExecute.ClassExecute dt;
    string str = webapp4U.Configuration.CurrentConnectionString;
    string error = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            
                HienThiTinh();
                HienThiQuan(DropTinh.SelectedValue.ToString());
                NoiCap();
                trinhdo();
                DanhHieu();
            
        }
    }
    void NoiCap()
    {
        dt = new ClassExecute.ClassExecute();
        DataTable db = dt.Get_Data_DataTable(str, ref error, "GET_DATA_PROVINCE", CommandType.StoredProcedure);
        DropNoiCap.DataSource = db;
        DropNoiCap.DataTextField = "ProvinceName";
        DropNoiCap.DataValueField = "ProvinceCode";
        DropNoiCap.DataBind();
       
    }
    void DanhHieu()
    { 
        dt = new ClassExecute.ClassExecute();
        DataTable db = dt.Get_Data_DataTable(str, ref error, "k_getHRSALELEVEL", CommandType.StoredProcedure);
        DropDH.DataSource = db;
        DropDH.DataTextField = "Description";
        DropDH.DataValueField = "Rate";
        DropDH.DataBind();
    }
    void trinhdo()
    {
        for (int i = 1; i < 13; i++)
        {
            DropTrinhDo.Items.Add(Convert.ToString(i) + "/12");
        }
        DropTrinhDo.SelectedIndex = DropTrinhDo.Items.Count - 1;
    }
    void HienThiTinh()
    {
        dt = new ClassExecute.ClassExecute();
        DataTable db = dt.Get_Data_DataTable(str, ref error, "GET_DATA_PROVINCE", CommandType.StoredProcedure);
        DropTinh.DataSource = db;
        DropTinh.DataTextField = "ProvinceName";
        DropTinh.DataValueField = "ProvinceCode";
        DropTinh.DataBind();
    }
    void HienThiQuan(string DropTinh)
    {
        dt = new ClassExecute.ClassExecute();
        DataTable db = dt.Get_Data_DataTable(str, ref error, "GET_DATA_DISTRICTOWN_PROVINCECODE", CommandType.StoredProcedure,
            new SqlParameter("@ProvinceCode", DropTinh));
        DropQuanHuyen.DataSource = db;
        DropQuanHuyen.DataTextField = "TownName";
        DropQuanHuyen.DataValueField = "TownCode";
        DropQuanHuyen.DataBind();
    }
    void clear()
    {
        txtMaTuVanVien.ReadOnly = true;
        txtMaTuVanVien.Text = "auto";
        txtNgayGiaNhap.Text = DateTime.Now.ToString("dd/MM/yyyy");
        txtRootAddress.Text = "";
        txtTenTuVanVien.Text = "";
        txtMaNguoiGioiThieu.Text = "";
        txtTenNguoiGioiThieu.Text = "";
        CheckConBanHang.Checked = false;
        txtSoCMND.Text = "";
        txtNgaySinh.Text = "";
        CheckGioiTinh.Checked = false;
        txtSoNha.Text = "";
        txtTenDuong.Text = "";
        txtDtCoDinh.Text = "";
        txtDTDiDong.Text = "";
        txtNgheNghiep.Text = "";
        CheckFullTime.Checked = true;
        txtMaNguoiGioiThieu.ReadOnly = false;
        txtTenNguoiGioiThieu.ReadOnly = false;
        CheckConBanHang.Checked = true;
    }
    void grid()
    {
        dt = new ClassExecute.ClassExecute();
        DataTable db = dt.Get_Data_DataTable(str, ref error, "K_FindInforTVV", CommandType.StoredProcedure,
            new SqlParameter("@parameter", txtSearch.Text),
            new SqlParameter("@sitecode",Session["sitecode"].ToString()));
        if (db.Rows.Count > 0)
        {
            GridView2.DataSource = db;
            GridView2.DataBind();
        }
       ViewState["flag"] = false;
    }
    protected void GridView2_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView2.PageIndex = e.NewPageIndex;
        grid();

    }
    protected void GridView2_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "chon")
        {
            GridViewRow grv = ((Control)e.CommandSource).NamingContainer as GridViewRow;
            dt = new ClassExecute.ClassExecute();

                try
                {
                    DataTable db=dt.Get_Data_DataTable(str,ref error,"K_GetInforBA",CommandType.StoredProcedure,
                        new SqlParameter("@salesp_key",grv.Cells[0].Text));
                    if (db.Rows.Count>0)
                    {
                        txtMaTuVanVien.Text = db.Rows[0]["salesp_key"].ToString();
                        txtTenTuVanVien.Text = db.Rows[0]["salesp_name"].ToString();
                        CheckConBanHang.Checked = bool.Parse(db.Rows[0]["Active"].ToString());
                        txtMaNguoiGioiThieu.Text = db.Rows[0]["Recommender"].ToString();
                        txtTenNguoiGioiThieu.Text = db.Rows[0]["GioiThieu"].ToString();
                        txtSoCMND.Text = db.Rows[0]["IDCard"].ToString();
                        try
                        {
                            DropNoiCap.SelectedValue = db.Rows[0]["IDWhere"].ToString();
                        }
                        catch
                        {

                        }
                        txtNgaySinh.Text = db.Rows[0]["BirthDate"].ToString();
                        CheckGioiTinh.Checked = bool.Parse(db.Rows[0]["Sex"].ToString());
                        txtSoNha.Text = db.Rows[0]["Address"].ToString();
                        txtTenDuong.Text = db.Rows[0]["Street"].ToString();
                        try
                        {
                            DropTinh.SelectedValue = db.Rows[0]["Province"].ToString();
                            HienThiQuan(DropTinh.SelectedValue.ToString());
                        }
                        catch
                        {

                        }
                        try
                        {
                            DropQuanHuyen.SelectedValue = db.Rows[0]["Town"].ToString();
                        }
                        catch
                        {
                        }
                        txtDtCoDinh.Text = db.Rows[0]["Tel"].ToString();
                        txtDTDiDong.Text = db.Rows[0]["Mobile"].ToString();
                        try
                        {
                            DropTrinhDo.SelectedItem.Value = db.Rows[0]["EducationLevel"].ToString();
                        }
                        catch
                        {
                        }
                        txtNgheNghiep.Text = db.Rows[0]["Job"].ToString();
                        CheckFullTime.Checked = bool.Parse(db.Rows[0]["FullTime"].ToString());
                        txtNgayGiaNhap.Text = db.Rows[0]["RecDate"].ToString();
                     
                        txtRootAddress.Text = db.Rows[0]["rootAddress"].ToString();
                        try
                        {
                            DropDH.SelectedValue = db.Rows[0]["SalesLevel"].ToString();
                        }
                        catch
                        {

                        }
                  
                        txtMaNguoiGioiThieu.ReadOnly = true;
                        txtTenNguoiGioiThieu.ReadOnly = true;
                        TabContainer1.ActiveTabIndex = 0;
                    }
                }
                catch
                {
                   
                }
           
        }
    }
    protected void ImgNew_Click(object sender, ImageClickEventArgs e)
    {
       
           ViewState["flag"] = true;
            TabContainer1.ActiveTabIndex = 0;
            clear();
       
    }
    protected void ImgSave_Click(object sender, ImageClickEventArgs e)
    {
        if (Regex.IsMatch(txtSoCMND.Text, @"^[0-9]{9,9}$"))
        {
            dt = new ClassExecute.ClassExecute();
            string kq = "ko";
            try
            {
                int i = dt.ExecuteSQl_int(str, "ls_isIDNoValid", CommandType.StoredProcedure, ref error,
                    new SqlParameter("@IDNo", txtSoCMND.Text),
                    new SqlParameter("@salespKey", txtMaTuVanVien.Text));
                if (i == 1)
                {
                    if ((bool)ViewState["flag"])
                    {

                        kq = dt.GetDataDepend(str, "ls_saveSalesPersonInfo", CommandType.StoredProcedure, ref error,
                        new SqlParameter("@salesPkey", "auto"),
                        new SqlParameter("@salesPName", txtTenTuVanVien.Text),
                        new SqlParameter("@birthDay", txtNgaySinh.Text),
                        new SqlParameter("@sex", CheckGioiTinh.Checked ? 1 : 0),
                        new SqlParameter("@rootAddress", txtRootAddress.Text),
                        new SqlParameter("@IDNo", txtSoCMND.Text),
                        new SqlParameter("@IDProvince", DropNoiCap.SelectedValue.ToString()),
                        new SqlParameter("@eduLevel", DropTrinhDo.SelectedItem.ToString()),
                        new SqlParameter("@cJob", txtNgheNghiep.Text),
                        new SqlParameter("@recDate", DateTime.Now.ToString("dd/MM/yyyy")),
                        new SqlParameter("@isActive", CheckConBanHang.Checked ? 1 : 0),
                        new SqlParameter("@salesTitle", DropDH.SelectedValue.ToString()),
                        new SqlParameter("@isFullTime", CheckFullTime.Checked ? 1 : 0),
                        new SqlParameter("@recommender", txtMaNguoiGioiThieu.Text),
                        new SqlParameter("@address", txtSoNha.Text),
                        new SqlParameter("@street", txtTenDuong.Text),
                        new SqlParameter("@province", DropTinh.SelectedValue.ToString()),
                        new SqlParameter("@town", DropQuanHuyen.SelectedValue.ToString()),
                        new SqlParameter("@tel", txtDtCoDinh.Text),
                        new SqlParameter("@mobile", txtDTDiDong.Text),
                        new SqlParameter("@siteCode", Session["sitecode"].ToString()));

                        if (kq != "ko")
                        {
                            
                            txtMaTuVanVien.Text = kq;
                            txtNgayGiaNhap.ReadOnly = true;
                        }
                        else
                        {
                           
                        }
                      ViewState["flag"] = false;
                    }
                    else
                    {
                        kq = dt.GetDataDepend(str, "ls_saveSalesPersonInfo", CommandType.StoredProcedure, ref error,
                           new SqlParameter("@salesPkey", txtMaTuVanVien.Text),
                           new SqlParameter("@salesPName", txtTenTuVanVien.Text),
                           new SqlParameter("@birthDay", txtNgaySinh.Text),
                           new SqlParameter("@sex", CheckGioiTinh.Checked ? 1 : 0),
                           new SqlParameter("@rootAddress", txtRootAddress.Text),
                           new SqlParameter("@IDNo", txtSoCMND.Text),
                           new SqlParameter("@IDProvince", DropNoiCap.SelectedValue.ToString()),
                           new SqlParameter("@eduLevel", DropTrinhDo.SelectedItem.ToString()),
                           new SqlParameter("@cJob", txtNgheNghiep.Text),
                           new SqlParameter("@recDate", txtNgayGiaNhap.Text),
                           new SqlParameter("@isActive", CheckConBanHang.Checked ? 1 : 0),
                           new SqlParameter("@salesTitle", DropDH.SelectedValue.ToString()),
                           new SqlParameter("@isFullTime", CheckFullTime.Checked ? 1 : 0),
                           new SqlParameter("@recommender", txtMaNguoiGioiThieu.Text),
                           new SqlParameter("@address", txtSoNha.Text),
                           new SqlParameter("@street", txtTenDuong.Text),
                           new SqlParameter("@province", DropTinh.SelectedValue.ToString()),
                           new SqlParameter("@town", DropQuanHuyen.SelectedValue.ToString()),
                           new SqlParameter("@tel", txtDtCoDinh.Text),
                           new SqlParameter("@mobile", txtDTDiDong.Text),
                           new SqlParameter("@siteCode", Session["sitecode"].ToString()));
                        if (kq != "ko")
                        {
                           
                            txtMaTuVanVien.Text = kq;
                        }
                        else
                        {
                            
                        }
                    }
                }
                else
                {
                   
                }
            }
            catch 
            {
                
            }
        }
    }   
    protected void DropTinh_SelectedIndexChanged1(object sender, EventArgs e)
    {
        HienThiQuan(DropTinh.SelectedValue.ToString());
    }
    protected void txtSearch_TextChanged(object sender, EventArgs e)
    {
       
            grid();
        
    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
       
            grid();
        
    }
    protected void ImgTimKiemTuVanVienChinhThuc_Click(object sender, ImageClickEventArgs e)
    {
        TabContainer1.ActiveTabIndex = 1;
    }
    protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            if ((bool)ViewState["flag"] == false)
            {
                ClassExecute.ClassExecute obj = new ClassExecute.ClassExecute();
                bool kq = obj.ExecuteSQl_bool(str, CommandType.StoredProcedure, "[ls_deleteBA]", ref error,
                    new SqlParameter("@selSalesPKey", txtMaTuVanVien.Text.Trim()));
                if (kq)
                {
                   
                }
                else
                {
                   
                }
            }
            else
            {
                
            }
        }
        catch
        {
           
        }
    }
    protected void txtMaNguoiGioiThieu_TextChanged(object sender, EventArgs e)
    {
        try
        {
            txtTenNguoiGioiThieu.Text = txtMaNguoiGioiThieu.Text.Substring(txtMaNguoiGioiThieu.Text.IndexOf(Convert.ToChar("|")) + 2);
            txtMaNguoiGioiThieu.Text = txtMaNguoiGioiThieu.Text.Substring(0, txtMaNguoiGioiThieu.Text.IndexOf(Convert.ToChar("|")));
        }
        catch 
        {
          
        }
    }
    
}
